package com.leessy.r229.server.http;

import android.util.Base64;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.EncryptUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSource;

public class AesEncryptInterceptor implements Interceptor {

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        // 拿到请求参数
        Request request = chain.request();

        //对响应进行解密
        try (Response response = chain.proceed(request)) {
            ResponseBody responseBody = response.body();

            byte[] contents = responseBody.bytes();

            byte[] decode = Base64.decode(contents, Base64.NO_WRAP);
            byte[] keys = ConvertUtils.hexString2Bytes("e4e3e1ef9ee249328c4a1b6a0293c4dae4e3e1ef9ee24932");
            byte[] enrypt = EncryptUtils.decrypt3DES(
                    decode,
                    keys,
                    "DESede/ECB/NoPadding",
                    null
            );

//            String decryptString = AESHelper.decrypt(responseBodyString, password);
            ResponseBody newResponseBody = ResponseBody.create( responseBody.contentType(),new String(enrypt));
            return response.newBuilder().body(newResponseBody).build();
        }
    }
}